[Snyk] Upgrade zod from 3.24.3 to 4.1.8 #61
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Snyk has created this PR to upgrade zod from 3.24.3 to 4.1.8.
ℹ️ Keep your dependencies up-to-date. This makes it easier to fix existing vulnerabilities and to more quickly identify and fix newly disclosed vulnerabilities when they affect your project.
The recommended version is 386 versions ahead of your current version.
The recommended version was released 24 days ago.
Issues fixed by the recommended upgrade:
SNYK-JS-ONHEADERS-10773729
SNYK-JS-BRACEEXPANSION-9789073
Release notes
Package name: zod
Commits:
Commits:
api.mdx(#5209)api.mdx(#5207)@ traversable/zodand@ traversable/zod-testto v4 ecosystem (#5194)Commits:
Commits:
nulloutput when targetingopenapi-3.0(#5156)validateOpenAPI30Schemain all relevant scenarios (#5163)Commits:
openapi-3.0(#5141)openapi-3.0(#5139)LooseDoginstead ofDog(#5136)Commits:
openapi-3.0(#5145)Commits:
The first minor version since the introduction of Zod 4 back in May. This version contains a number of features that barely missed the cut for the 4.0 release. With Zod 4 stable and widely adopted, there's more time to resume feature development.
Codecs
This is the flagship feature of this release. Codecs are a new API & schema type that encapsulates a bi-directional transformation. It's a huge missing piece in Zod that's finally filled, and it unlocks some totally new ways to use Zod.
New top-level functions are added for processing inputs in the forward direction ("decoding") and backward direction ("encoding").
// => Date
stringToDate.encode(new Date())
// => "2025-08-21T20:59:45.500Z"
.parse()vs.decode()Both
.parse()anddecode()process data in the "forward" direction. They behave identically at runtime.There is an important difference however. While
.parse()accepts any input,.decode()expects a strongly typed input. That is, it expects an input of typestring, whereas.parse()acceptsunknown.// => fails at runtime, but no TypeScript error
stringToDate.decode(Symbol("not-a-string"));
// ^ ❌ Argument of type 'symbol' is not assignable to parameter of type 'Date'. ts(2345)
Encoding
You can use any Zod schema with
.encode(). The vast majority of Zod schemas are non-transforming (the input and output types are identical) so.decode()and.encode()behave identically. Only certain schema types change their behavior:B->Aand executes theencodetransform during encodingB->Ainstead ofA->BThe usual async and safe variants exist as well:
stringToDate.decode("2024-01-15T10:30:00.000Z")
await stringToDate.decodeAsync("2024-01-15T10:30:00.000Z")
stringToDate.safeDecode("2024-01-15T10:30:00.000Z")
await stringToDate.safeDecodeAsync("2024-01-15T10:30:00.000Z")
// encode methods
stringToDate.encode(new Date())
await stringToDate.encodeAsync(new Date())
stringToDate.safeEncode(new Date())
await stringToDate.safeEncodeAsync(new Date())
Example codecs
Below are some "worked examples" for some commonly-needed codecs. These examples are all tested internally for correctness. Just copy/paste them into your project as needed. There is a more comprehensive set available at zod.dev/codecs.
stringToBigIntConverts
bigintinto a serializable form.const stringToBigInt = z.codec(z.string(), z.bigint(), {decode: (str) => BigInt(str),
encode: (bigint) => bigint.toString(),
});
stringToBigInt.decode("12345"); // => 12345n
stringToBigInt.encode(12345n); // => "12345"
jsonParses/stringifies JSON data.
To further validate the data,
.pipe()the result of this codec into another schema.const JsonToParams = jsonCodec.pipe(Params);
JsonToParams.decode('{"name":"Alice","age":30}'); // => { name: "Alice", age: 30 }
JsonToParams.encode({ name: "Bob", age: 25 }); // => '{"name":"Bob","age":25}'
Further reading
For more examples and a technical breakdown of how encoding works, reads theannouncement blog post and new Codecs docs page. The docs page contains implementations for several other commonly-needed codecs:
stringToNumberstringToIntstringToBigIntnumberToBigIntisoDatetimeToDateepochSecondsToDateepochMillisToDatejsonCodecutf8ToBytesbytesToUtf8base64ToBytesbase64urlToByteshexToBytesstringToURLstringToHttpURLuriComponentstringToBoolean.safeExtend()The existing way to add additional fields to an object is to use
.extend().Unfortunately this is a bit of a misnomer, as it allows you to overwrite existing fields. This means the result of
.extend()may not literallyextendthe original type (in the TypeScript sense).To enforce true
extendslogic, Zod 4.1 introduces a new.safeExtend()method. This statically enforces that the newly added properties conform to the existing ones.Importantly, this new API allows you to safely extend objects containing refinements.
Previously (in Zod 4.x) any refinements attached to the base schema were dropped in the extended result. This was too unexpected. It now throws an error. (Zod 3 did not support extension of refined objects either.)
z.hash()A new top-level string format for validating hashes produced using various common algorithms & encodings.
// => ZodCustomStringFormat<"md5_hex">
const sha256Base64 = z.hash("sha256", { enc: "base64" });
// => ZodCustomStringFormat<"sha256_base64">
The following hash algorithms and encodings are supported. Each cell provides information about the expected number of characters/padding.
"hex""base64""base64url""md5""sha1""sha256""sha384""sha512"z.hex()To validate hexadecimal strings of any length.
hexSchema.parse("123abc"); // ✅ "123abc"
hexSchema.parse("DEADBEEF"); // ✅ "DEADBEEF"
hexSchema.parse("xyz"); // ❌ ZodError
Additional changes
FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF) per the RFC$ZodFunctionis now a subtype of$ZodTypeCommits